<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Poll extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->model('poll_model');
		$this->form_validation->set_error_delimiters('<span class="error"></span>');
	}

	public function index()
	{
		$this->main();
	}

	
	function create_location()
	{
		$this->data->address = $this->misc_model->create_address_array();
		$this->load->view('create_location', $this->data);
	}
	
	function create()
	{

		if($this->form_validation->run('poll'))
		{
			
		}
		
		$this->data->head->title = "설문작성";
		$this->data->address = $this->misc_model->create_address_array();
		$this->load->view('poll/create', $this->data);
	}
	
	function main()
	{
		$this->load->view('poll/main', $this->data);
	}
	
	function search()
	{
		$get = $this->input->get();

		if( empty($get) )
		{
			$this->data->address = $this->misc_model->create_address_array();
			$this->load->view('poll/search', $this->data);
			return;
		}

		$opts = array(
			'title' => "제목",
			'education' => "학력",
			'address_id' => "거주지",
			'gender' => "성별",
			'age_range' => "연령대",
			'location' => "위치",
			'keyword' => "키워드",
			'category' => "카테고리"
		);
		
		if( ! empty($get['address_id']))
			$get['address_id'] = max($get['address_id']);
		$search_expired = $get['search-expired'];
		
		$this->load->helper('array');
		$get = elements(array_keys($opts), $get);
		
		$res = $this->poll_model->search($get);
		if($search_expired)
			$res += $this->poll_model->search($get, TRUE);
		
		$this->data->head->title = "검색결과";
		$this->data->s_results = $res;
		$this->load->view('poll/search_result', $this->data);
	}
	
	function vote($id = NULL)
	{
		if($this->form_validation->run('poll'))
		{
			
		}
		//id
		$this->data->poll = $this->poll_model->get_entire_poll_by_id($id);
		$this->data->head->title = "설문하기";
		$this->data->address = $this->misc_model->create_address_array();
		$this->data->username = $this->user_model->get_user_by_id($this->data->poll->user_id);
		$this->misc_model->increase_view_count($id);
		$this->load->view('poll/vote', $this->data);

	}

	function result_question($id = NULL)
	{
		$this->_add_js('result');
		$this->data->head->title = "설문결과";
		$this->data->poll = $this->poll_model->get_entire_poll_by_id($id);
		$votes = $this->poll_model->get_result_votes($id);
		$this->data->username = $this->user_model->get_user_by_id($this->data->poll->user_id);

		//배열로 answers 가져오기
		$answers_array[] = array();
		$size_answers = count($votes[0]['answers']);
		for($i=0;$i<$size_answers;$i++){
			foreach($votes as $ans){
				$answers_array[$i][] = $ans['answers'][$i];	
			}
			$array_cnt[] = (array_count_values($answers_array[$i]));
		}
		
		//vote 추가
		//설문 - 성별,학력,연령대 분포
		$cnt = new stdClass;

		if( ! empty($this->data->poll->conditions))
		{
			$cnt->male = 0;
			$cnt->female = 0;
			$cnt->education = array(0,0,0,0,0);
			$cnt->years = array(0,0,0,0,0,0,0,0,0);
			
			foreach($votes as $vote)
			{
				if($vote['gender'] =='male')
					$cnt->male++;	
				if($vote['gender'] =='female')
					$cnt->female++;
				
				switch($vote['education']){	
					case '0':
						$cnt->education[0]++;
						break;
					case '1':
						$cnt->education[1]++;
						break;
					case '2':
						$cnt->education[2]++;
						break;
					case '3':
						$cnt->education[3]++;
						break;
					case '4':
						$cnt->education[4]++;					
						break;
					default:
						echo 'invalid education';
	 				}
				
				//생일을 통한 연령대
				$years = (integer)((floor((time()-strtotime($vote['birthday'])) / 31536000)+1)/10);
				$cnt->years[max($years-1, 0)]++;
			}	
		}
		//추가끝
		
		$this->data->cnt = $array_cnt; //항목별 답변에 관한 DATA
		$this->data->cnt_vote = $cnt; //설문에 참여한 사람들의 비율(성별, 연령대, 학력) DATA
		$this->load->view('poll/result_question', $this->data);
	}
	
	function delete($id = NULL)
	{
		$this->poll_model->delete_poll_by_id($id);
		$this->data->poll_list = $this->poll_model->get_all_polls();
		$this->load->view('admin/poll_list', $this->data);		
	}
}

/* End of file poll.php */
/* Location: ./application/controllers/poll.php */